package com.jie.langlangmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.dao.OmsOrderDao;
import com.jie.langlangmall.dao.OmsOrderOperateHistoryDao;
import com.jie.langlangmall.dto.*;
import com.jie.langlangmall.mapper.OmsOrderMapper;
import com.jie.langlangmall.mapper.OmsOrderOperateHistoryMapper;
import com.jie.langlangmall.model.OmsOrder;
import com.jie.langlangmall.model.OmsOrderExample;
import com.jie.langlangmall.model.OmsOrderOperateHistory;
import com.jie.langlangmall.service.OmsOrderService;
import com.jie.langlangmall.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Jie
 * @Date 2024/11/28
 * @Email 3217998214@qq.com
 * @Description 订单管理
 */
@Service
public class OmsOrderServiceImpl implements OmsOrderService {

    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private OmsOrderDao orderDao;
    @Autowired
    private OmsOrderOperateHistoryMapper orderOperateHistoryMapper;
    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private OmsOrderOperateHistoryDao orderOperateHistoryDao;


    /**
     * 查询订单
     * @param queryParam
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<OmsOrder> list(OmsOrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return orderDao.getList(queryParam);
    }

    /**
     * 订单发货
     * @param deliveryParamList
     */
    @Override
    @Transactional
    public void delivery(List<OmsOrderDeliveryParam> deliveryParamList) {
        // 发货
        orderDao.delivery(deliveryParamList);
        // 添加操作记录
        List<OmsOrderOperateHistory> operateHistoryList = deliveryParamList.stream()
                .map(omsOrderDeliveryParam -> {
                    OmsOrderOperateHistory history = new OmsOrderOperateHistory();
                    history.setOrderId(omsOrderDeliveryParam.getOrderId());
                    history.setCreateTime(new Date());
                    history.setOperateMan("后台管理员：" + adminService.getCurrentAdmin().getUsername());
                    history.setOrderStatus(2);
                    history.setNote("完成发货");
                    return history;
                }).collect(Collectors.toList());
        orderOperateHistoryDao.insertList(operateHistoryList);
    }

    /**
     * 关闭订单
     * @param ids
     * @param note
     */
    @Override
    public void close(List<Long> ids, String note) {
        OmsOrder record = new OmsOrder();
        record.setStatus(4);
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andDeleteStatusEqualTo(0).andIdIn(ids);
        int count = orderMapper.updateByExampleSelective(record, example);
        List<OmsOrderOperateHistory> historyList = ids.stream().map(orderId -> {
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员" + adminService.getCurrentAdmin().getUsername());
            history.setOrderStatus(4);
            history.setNote("订单关闭:" + note);
            return history;
        }).collect(Collectors.toList());
        orderOperateHistoryDao.insertList(historyList);

    }

    /**
     * 删除订单
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        OmsOrder record = new OmsOrder();
        record.setDeleteStatus(1);
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andIdIn(ids);
        orderMapper.deleteByExample(example);
    }

    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @Override
    public OmsOrderDetail detail(Long id) {
        return orderDao.getDetail(id);
    }

    /**
     * 修改收货人信息
     * @param receiverInfoParam
     */
    @Override
    @Transactional
    public void updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(receiverInfoParam.getOrderId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setModifyTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(receiverInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员：" + adminService.getCurrentAdmin().getUsername());
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        orderOperateHistoryMapper.insert(history);
    }

    /**
     * 修改费用信息
     * @param moneyInfoParam
     */
    @Override
    @Transactional
    public void updateMoneyInfo(OmsMoneyInfoParam moneyInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        order.setModifyTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员：" + adminService.getCurrentAdmin().getUsername());
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        orderOperateHistoryMapper.insert(history);
    }

    @Transactional
    @Override
    public void updateNote(Long id, String note, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(id);
        order.setNote(note);
        order.setModifyTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员：" + adminService.getCurrentAdmin().getUsername());
        history.setOrderStatus(status);
        history.setNote("修改备注信息：" + note);
        orderOperateHistoryMapper.insert(history);
    }

}
